<?php
/**
 * User: zoudingyong <sheyingtg@qq.com>
 * Date: 2019-9-16 15:35
 */

use think\Db;
use service\ToolsService;

/**
 * 获取限量分类资讯
 * @param $db
 * @param int $cate_id
 * @param string $order
 * @param int $limit
 * @param string $field
 * @return array|PDOStatement|string|\think\Collection
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function new_list($db = '', $cate_id = 0, $order = 'hits desc', $limit = 5, $field = 'cate_id')
{
    $where = [];
    if ($cate_id > 0) {
        $where[] = [$field, '=', $cate_id];
    }

    $list = Db::name($db)
        ->field('info_id,title,expire_at,create_at')
        ->where($where)
        ->limit($limit)
        ->order($order)
//        ->cache($db . $cate_id . $order . $limit . $field . '_new_list',5)
        ->select();
    return $list;
}


/**
 * 获取全部分类
 * @param string $db
 * @return array|mixed
 */
function get_all_cate($db = '')
{
//    $cate = cache($db . 'get_all_cate');
//    if (empty($cate)) {
    $list = Db::name($db)
        ->field('cate_id,name')
        ->where('is_open', 1)
        ->where('pid', 0)
        ->order('sort desc')
        ->select();
    $data = [];
    foreach ($list as $one) {
        $one['sub'] = Db::name($db)
            ->field('cate_id,name')
            ->where('is_open', 1)
            ->where('pid', $one['cate_id'])
            ->order('sort desc')
            ->select();
        $data[] = $one;
    }
//        cache($db . 'get_all_cate', $data, 5);
//        $cate = $data;
//    }
    return $data;
//    return $cate;
}

